import axios from "axios";
import { Message } from 'element-ui';

const apiRequest = axios.create({
    baseURL: ""
});

// apiRequest.interceptors.response.use(function(response) {
//     if(response){
//       if('000005' === response.data.code){
//         window.location.href = "/sc_tools/portal/#/login";
//       }
//       else
//         return response;
//     }
//   });

export const api = {
    "check_login": "mbs_test_main/user/check_login/",
    "google_login": "mbs_test_main/user/google_login/",
    "google_logout": "mbs_test_main/user/google_logout/",
    "jira_view_task": "mbs_test_main/jira/view_task/",
    "coverage_view": "mbs_test_main/coverage/view",
    "coverage_sum": "mbs_test_main/coverage/sum",
    "get_no_staging_bugs": "mbs_test_main/jira/get_no_staging_bugs",
    "get_no_end_bugs": "mbs_test_main/jira/get_no_end_bugs",
    "add_label": "mbs_test_main/jira/add_label/",
    //CAP
    "get_cap_info": "mbs_test_main/thirdplatform/get_cap_info", //获取CAP的信息
    "get_master_cap_info": "mbs_test_main/thirdplatform/get_master_cap_info", //获取CAP的信息
    "sched_cap_info": "mbs_test_main/thirdplatform/sched_cap_info", //刷新CAP的信息
    //SCP
    "get_all_version_list": "mbs_test_main/scpplatform/get_all_version_list",//获取SCP的信息
    "get_version_scp_info": "mbs_test_main/scpplatform/get_version_scp_info",//获取SCP的信息
    "scp_notice": "mbs_test_main/scpplatform/scp_notice",//消息触发

    //CRM&MBS小工具接口
    "change_message_qutoa": "mbs_test_api/crm/add_message_quota",
    "change_broadcast_qutoa": "mbs_test_api/crm/add_broadcast_quota",
    "create_account": "mbs_test_api/user/create",
    "get_info": "mbs_test_api/user/getinfo",
    "query_db": "mbs_test_api/universal/db",
    "query_redis": "mbs_test_api/universal/redis",
    "del_redis": "mbs_test_api/universal/redis_del",
    "add_follower": "mbs_test_api/follow/add",
    "add_point_mock_order": "mbs_test_api/membership/add_point_mock_order",
    "add_point_by_json": "mbs_test_api/membership/add_point_by_json",
    "query_map_point": "mbs_test_api/membership/query_map_point",
    "refresh_timing_task": "mbs_test_api/membership/refresh_timing_task",
    "be_penalty": "mbs_test_api/crm/be_penalty",
    "be_warning": "mbs_test_api/crm/be_warning",
    "escape_from_penalty": "mbs_test_api/crm/escape_from_penalty",
    "create_shop_voucher": "/mbs_test_api/mkt_tools/create_shop_voucher",
    "create_product_voucher": "/mbs_test_api/mkt_tools/create_product_voucher",
    "create_discount_activity": "/mbs_test_api/mkt_tools/create_discount_activity",
    "create_addon_deal": "/mbs_test_api/mkt_tools/create_addon_deal",
    "create_bundle_deal": "/mbs_test_api/mkt_tools/create_bundle_deal",
    "mock_udp_data": "/mbs_test_api/crm_data/mock_udp_data",
    "join_membership": "/mbs_test_api/membership/join_membership",
    "quit_membership": "/mbs_test_api/membership/quit_membership",
    "create_voucher_mbs": "/mbs_test_api/membership/create_voucher_mbs",
    "update_profile_phone_num":"/mbs_test_api/membership/update_phone_num",
    //下面是SchechX项目的mock接口
    //项目配置
    "project_list": "schedule_x/project/list", //获取项目列表
    "global_param_list": "schedule_x/project/global_param_list", //全局变量列表
    "global_param_add": "schedule_x/project/global_param_add", //新增全局变量
    "global_param_del": "schedule_x/project/global_param_del", //删除全局变量

    "get_global": "schedule_x/task/get_global", //查看项目配置
    "update_global": "schedule_x/task/update_global", //修改项目配置


    //任务
    "task_list": "schedule_x/task/list", //任务列表
    "task_view": "schedule_x/task/view", //查看配置
    "task_execute": "schedule_x/task/execute", //任务执行
    "task_online": "schedule_x/task/online", //任务上线
    "task_offline": "schedule_x/task/offline", //任务下线
    "task_execute_record_list": "schedule_x/task/execute_record_list", //任务执行历史列表
    "task_execute_record_log": "schedule_x/task/execute_record_log", //查看指定任务log
    "get_unrelease_version": "schedule_x/task/get_unrelease_version", //获取待发布版本
    "get_project_config": "schedule_x/task/get_project_config", //获取项目全局配置接口
    "update_step_config": "schedule_x/task/update_step_config",

    //任务实例
    "task_instance_list": "schedule_x/task_instance/list", //任务实例列表
    "task_instance_end": "schedule_x/task_instance/end", //实例停止
    "task_instance_resume": "schedule_x/task_instance/resume", //实例恢复
    "task_instance_view": "schedule_x/task_instance/view", //查看任务实例基本信息
    "task_instance_display_list": "schedule_x/task_instance/display_list", //查看任务实例展示列表
    "task_instance_deploy_list": "schedule_x/task_instance/deploy_list", //查看任务实例部署列表
    "task_instance_step_list": "schedule_x/task_instance/step_list", //查看任务实例步骤列表
    "task_instance_param_list": "schedule_x/task_instance/param_list", //任务实例变量列表
    "task_instance_get_by_step_id": "schedule_x/task_instance/get_by_step_id", //根据步骤获取实例
    "task_instance_refresh_version_info": "schedule_x/task_instance/refresh_version_info", //更新版本信息
    "task_instance_refresh_task_info": "schedule_x/task_instance/refresh_task_info", //更新任务信息
    "task_instance_refresh_deploy_info": "schedule_x/task_instance/refresh_task_info", //更新部署信息

    //步骤
    "step_get_data": "schedule_x/step/get_data", //步骤信息
    "step_execute": "schedule_x/step/execute", //步骤执行
    "step_skip": "schedule_x/step/skip", //步骤跳过
    "step_update": "schedule_x/step/update", //修改时间
    "step_execute_record_list": "schedule_x/step/execute_record_list", //查看执行历史列表
    "step_execute_record_log": "schedule_x/step/execute_record_log", //查看执行历史日志
    "step_all_done": "schedule_x/step/all_done", //全部完成
    "step_all_skip": "schedule_x/step/all_skip", //全部跳过
    "step_remind": "schedule_x/step/remind", //催办
    "step_get_result": "schedule_x/step/get_result", //获取执行结果
    "step_get_marked_content": "schedule_x/step/get_marked_content", // 获取markdown文件内容

    //待办
    "todo_list": "schedule_x/todo/list", //待办列表
    "todo_skip": "schedule_x/todo/skip", //跳过
    "todo_done": "schedule_x/todo/done", //完成
    "todo_forward": "schedule_x/todo/forward", //转发
    "todo_view_my_task": "schedule_x/todo/view_my_task", //获取我的关联任务

    //权限
    "permission_user": "schedule_x/permission/user_get", //获取当前用户权限级别
    "permission_project_list": "schedule_x/permission/project_list", //获取当前项目权限列表
    "permission_project_user_list": "schedule_x/permission/project_user_list", //获取当前项目下用户权限列表
    "permission_project_add": "schedule_x/permission//project_add", //增加项目
    "permission_user_add": "schedule_x/permission/user_add", //增加人员权限接口
    "permission_user_edit": "schedule_x/permission/user_edit", //修改人员权限接口
    "permission_user_delete": "schedule_x/permission/user_delete", //修改人员权限接口

    //接口自动化平台工具
    "template_task_list": "mbs_test_main/apitest/template_task_list", //查询所有项目模块
    "create_template_task": "mbs_test_main/apitest/create_template_task", //创建项目模块
    "edit_template_task": "mbs_test_main/apitest/edit_template_task", //编辑项目模块
    "create_api_test": "mbs_test_main/apitest/create_api_test", //创建自动化任务
    "task_history": "mbs_test_main/apitest/task_history", //查询单个项目模块下的历史记录
    "sycn_result": "mbs_test_main/apitest/sycn_result", //查询db里result_id ！=0的数据，进行数据同步
    "get_pfb": "mbs_test_main/apitest/get_pfb_mbs", //查询业务当前存在的pfb
    "stop_task": "mbs_test_main/apitest/stop_task", //停止任务，不再通知

    //notice通知功能
    "add_noti_task": "mbs_test_api/thirdparty/add_noti_task", //增加任务
    "delete_noti_task": "mbs_test_api/thirdparty/delete_noti_tasks", //删除任务，不再通知
    "noti_task": "mbs_test_api/thirdparty/execute_noti_task_now", //立刻通知
    "get_noti_task": "mbs_test_api/thirdparty/get_noti_task_list", //查询通知任务
    "get_jira_list": "mbs_test_api/thirdparty/get_jira_task_list", //查询符合要求的jira单
}

var resultReturn = ""
var isLoadingResult = ""

const postURL = function(post_url, params) {
    return new Promise(function(resolve, reject) {
        apiRequest
            .post(post_url, params)
            .then(res => {
                resultReturn = res.data
                isLoadingResult = false
                if (res.status == '200') {
                    Message({
                        showClose: true,
                        message: "操作成功！",
                        type: "success",
                        duration: 10000
                    });
                    resolve(resultReturn)
                } else {
                    reject(resultReturn)
                }
            }, (err) => {
                resultReturn = err.response.data
                isLoadingResult = false

                if (err.response.status == 403 && post_url == api["login"]) {
                    Message({
                        showClose: true,
                        message: "账号密码错误，请重试",
                        type: "error"
                    });
                } else if (err.response.status == 402) {
                    location.reload()
                } else {
                    Message({
                        showClose: true,
                        message: "操作失败，请重试！",
                        type: "error"
                    });
                }

                reject(resultReturn)
            })
    })
}

const getURL = function(get_url, params) {
    return new Promise(function(resolve, reject) {
        apiRequest
            .get(get_url, {
                params
            })
            .then(res => {
                resultReturn = res.data
                if (res.status == '200') {
                    resolve(resultReturn)
                } else {
                    reject(resultReturn)
                }
            }, (err) => {
                resultReturn = err.response.data

                if (err.response.status == 401) {
                    Message({
                        showClose: true,
                        message: resultReturn.msg,
                        type: "error"
                    });
                } else if (err.response.status == 402) {
                    location.reload()
                } else {
                    Message({
                        showClose: true,
                        message: "请求失败，请重试",
                        type: "error",
                        duration: 10000
                    });
                }

                reject(resultReturn)
            })
    })
}

export async function postRequest(url, params) {
    await postURL(url, params)

    return { resultReturn, isLoadingResult }
}

export async function getRequest(url, params) {
    await getURL(url, params)

    return { resultReturn }
}
